package com.sahyadri.web.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

public class UniqueTimestamp {

	private static final AtomicLong LAST_TIME_MS = new AtomicLong();
	
	public static long uniqueCurrentTimeMS() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		long now = new Long(sdf.format(new Date())).longValue();
	    while(true) {
	        long lastTime = LAST_TIME_MS.get();
	        if (lastTime >= now)
	            now = lastTime+1;
	        if (LAST_TIME_MS.compareAndSet(lastTime, now))
	            return now;
	    }
	}
	
	public static void main(String[] args) {
		for(int i=1;i<30;i++) {
			System.out.println(UniqueTimestamp.uniqueCurrentTimeMS());
			
		}
	}
}
